package com.freework.sys.dic.vo;

import java.sql.Connection;
import java.util.Collection;

import javax.annotation.Resource;

import com.freework.freedbm.base.DTO;
import com.freework.freedbm.cfg.fieldcfg.type.LinkType;
import com.freework.freedbm.cfg.tablecfg.TableDataManager;
import com.freework.freedbm.dao.jdbcm.JdbcForDTO;
import com.freework.freedbm.util.DTOByCfg;
import com.freework.sys.dic.service.DicServiceI;
import com.freework.sys.permissions.external.exception.AppException;
import com.freework.sys.table.service.TableServiceI;
import com.freework.sys.table.vo.SysDictableVO;
import com.freework.util.SpringBeanUtil;
import com.freework.util.SqlUtil;

public class DicLinkType implements LinkType {

	String sourcetable = null;
	String sourcecolumn = null;
	String columnname = null;
	private static final Integer one = new Integer(1);

	
	public DicLinkType(String sourcetable, String sourcecolumn,
			String columnname) {
		super();
		this.sourcetable = sourcetable;
		this.sourcecolumn = sourcecolumn;
		this.columnname = columnname;
	}

	static TableServiceI tableService = (TableServiceI) SpringBeanUtil.bean()
			.getBean("dic.table.service.TableService");
	static DicServiceI service = (DicServiceI) SpringBeanUtil.bean().getBean(
			"dic.service.DicService");;

	public Object getDTOValue(Object dto, JdbcForDTO[] jdbcForDTOs) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getDTOValue(Object dto, JdbcForDTO[] jdbcForDTOs,
			Connection con) {

		TableDataManager manager = DTOByCfg.getTableDataManager((DTO) dto);
		Object value = manager.getField(columnname).getValue(dto);
		if (value != null) {
			SysDictableVO table = tableService.findTableByName(sourcetable);
			if (table == null)
				return null;
			boolean isKeycolumn = false;
			boolean isCodecolumn = false;
			if (sourcecolumn != null&& !sourcecolumn.toLowerCase().equals(table.getKeycolumn().toLowerCase())) {
				if (sourcecolumn.toLowerCase().equals(table.getCodecolumn().toLowerCase())) {
					isKeycolumn = false;
					isCodecolumn = true;
				}
			} else {
				isKeycolumn = true;
			}

			if (one.equals(table.getCachetag())) {
				DicVO dic = null;
				try {
					if (isKeycolumn) {
						dic = service.findTableDateById(sourcetable, value);
					} else if (isCodecolumn) {
						dic = service.findTableDateByCode(sourcetable, value);
					}else{
						 Collection<DicVO> dicc=service.findTableDate(sourcetable);
						 for (DicVO dicVO : dicc) {
							if(value .equals(dicVO.get(sourcecolumn))){
								dic =dicVO;
								break;
							}
						}
					}
				} catch (AppException e) {
					e.printStackTrace();
				}
				if (dic == null)
					return null;
				else
					return dic.getName();

			}else{
				return SqlUtil.querySingle(con, sourcetable,table.getNamecolumn(), sourcecolumn+"='"+value+"'");
				
			}

		}
		return null;
	}

	public boolean isConnection() {
		return true;
	}

	public String getName() {
		return "DicLinkType";
	}

	public Class getReturnedClass() {
		return String.class;
	}

	public boolean isCollectionType() {
		return false;
	}

}
